package XmlLoader;
import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/* class FGObjectTrajectoryStructure : store trajectory information of one object */
public class FGObjectTrajectoryStructure 
{
	protected int nNumberOfVideoFrame = -1;
	protected int nNumberOfSynopsisFrame = -1;
	protected int nNumberOfPoint = -1;
	protected int nobjNewNumIndex = -1;
	protected int nNumberOfObject = -1;
	protected int variance_1 = -1;
	protected int variance_2 = -1;
	protected int variance_3 = -1;
	protected List<Double> list_gaussian_mean_1 = null;
	protected List<Double> list_gaussian_mean_2 = null;
	protected List<Double> list_gaussian_mean_3 = null;
	protected List<FGObjectStructure> list_fGObject = null;
    public FGObjectTrajectoryStructure() 
    {
    	//null
    }
    /* Function _001 : let data in node store in list_gaussian_mean */
    protected void _001_readGaussian(Node node, List<Double> list_gaussian_mean)
    {
    	int curIndex = 1;
    	NodeList childNodes = node.getChildNodes();
    	while(curIndex < childNodes.getLength())
    	{
    		if(childNodes.item(curIndex).getNodeName() == "double")
    		{
    			Node node_temp = childNodes.item(curIndex).getChildNodes().item(0);
    		    list_gaussian_mean.add(Double.parseDouble(node_temp.getNodeValue()));
    		}
    		curIndex = curIndex + 1;
    	}
    }
    /* Function _002 : read each FGObject in node and store those to list_FGObject */
    protected void _002_readListFGObject(Node node, List<FGObjectStructure> list_FGObject)
    {
    	int curIndex = 0;
    	NodeList childNodes = node.getChildNodes();
    	while(curIndex < childNodes.getLength())
    	{
    		if(childNodes.item(curIndex).getNodeName() == "FGObject")
    		{
		    	FGObjectStructure newFGObj = new FGObjectStructure();
		    	newFGObj.ACT001_storeInfo(childNodes.item(curIndex));
		    	list_FGObject.add(newFGObj);
    		}
	    	curIndex = curIndex + 1;
    	}
    }
    /* Function _003 : show information of list-gaussian_mean */
    protected void _003_show_list_gaussian_mean(List<Double> list)
    {
    	int index = 0;
    	while(list != null && index < list.size())
    	{
    		System.out.println("  " + list.get(index));
    		index = index + 1;
    	}
    }
    /* Function _004 : show information of list-fGObject */
    protected void _004_show_list_fGObject(List<FGObjectStructure> list)
    {
    	int index = 0;
    	while(index < list.size())
    	{
    		System.out.println("    " + "Point " + index);
    		list.get(index).ACT002_showInfo();
    		index = index + 1;
    	}
    }
    /* Function ACT001 : store all data of nodeList to this object */
    public void ACT001_storeInfo(NodeList nodeList)
    {
    	int curReadIndex = 0;
    	String curTitle = null;
    	Node curNode = null, curNodeFirstItem = null;
    	while(curReadIndex < nodeList.getLength())
    	{
    		curNode = nodeList.item(curReadIndex);
    		curTitle = curNode.getNodeName();
    		curNodeFirstItem = curNode.getChildNodes().item(0);
    		if(curTitle.compareTo("nNumberOfVideoFrame") == 0) //read nNumberOfVideoFrame
    		{
    			this.nNumberOfVideoFrame = Integer.parseInt(curNodeFirstItem.getNodeValue());
    		}
    		else if(curTitle.compareTo("nNumberOfSynopsisFrame") == 0) //read nNumberOfSynopsisFrame
    		{
    			this.nNumberOfSynopsisFrame = Integer.parseInt(curNodeFirstItem.getNodeValue());
    		}
    		else if(curTitle.compareTo("nNumberOfPoint") == 0) //read nNumberOfPoint
    		{
    			this.nNumberOfPoint = Integer.parseInt(curNodeFirstItem.getNodeValue());
    		}
    		else if(curTitle.compareTo("nobjNewNumIndex") == 0) //read nobjNewNumIndex
    		{
    			this.nobjNewNumIndex = Integer.parseInt(curNodeFirstItem.getNodeValue());
    		}
    		else if(curTitle.compareTo("nNumberOfObject") == 0) //read nNumberOfObject
    		{
    			this.nNumberOfObject = Integer.parseInt(curNodeFirstItem.getNodeValue());
    		}
    		else if(curTitle.compareTo("variance_1") == 0) //read variance_1
    		{
    			this.variance_1 = Integer.parseInt(curNodeFirstItem.getNodeValue());
    		}
    		else if(curTitle.compareTo("variance_2") == 0) //read variance_2
    		{
    			this.variance_2 = Integer.parseInt(curNodeFirstItem.getNodeValue());
    		}
    		else if(curTitle.compareTo("variance_3") == 0) //read variance_3
    		{
    			this.variance_3 = Integer.parseInt(curNodeFirstItem.getNodeValue());
    		}
    		else if(curTitle.compareTo("gaussian_mean_1") == 0) //read gaussian_mean_1
    		{
    			this.list_gaussian_mean_1 = new ArrayList<Double>();
    			this._001_readGaussian(curNode, this.list_gaussian_mean_1);
    		}
    		else if(curTitle.compareTo("gaussian_mean_2") == 0) //read gaussian_mean_2
    		{
    			this.list_gaussian_mean_2 = new ArrayList<Double>();
    			this._001_readGaussian(curNode, this.list_gaussian_mean_2);
    		}
    		else if(curTitle.compareTo("gaussian_mean_3") == 0) //read gaussian_mean_3
    		{
    			this.list_gaussian_mean_3 = new ArrayList<Double>();
    			this._001_readGaussian(curNode, this.list_gaussian_mean_3);
    		}
    		else if(curTitle.compareTo("FGObjectArray") == 0) //read FGObjectArray
    		{
    			this.list_fGObject = new ArrayList<FGObjectStructure>();
    			this._002_readListFGObject(curNode, this.list_fGObject);
    		}
	    	curReadIndex = curReadIndex + 1;
    	}
    }
    /* Function ACT002 : show all attribute value of this object for testing */
    public void ACT002_showInfo()
    {
    	System.out.println("nNumberOfVideoFrame = " + this.nNumberOfVideoFrame);
    	System.out.println("nNumberOfSynopsisFrame = " + this.nNumberOfSynopsisFrame);
    	System.out.println("nNumberOfPoint = " + this.nNumberOfPoint);
    	System.out.println("nobjNewNumIndex = " + this.nobjNewNumIndex);
    	System.out.println("nNumberOfObject = " + this.nNumberOfObject);
    	System.out.println("variance_1 = " + this.variance_1);
    	System.out.println("variance_2 = " + this.variance_2);
    	System.out.println("variance_3 = " + this.variance_3);
    	System.out.println("gaussian_mean_1 :");
    	this._003_show_list_gaussian_mean(this.list_gaussian_mean_1);
    	System.out.println("gaussian_mean_2 :");
    	this._003_show_list_gaussian_mean(this.list_gaussian_mean_2);
    	System.out.println("gaussian_mean_3 :");
    	this._003_show_list_gaussian_mean(this.list_gaussian_mean_3);
    	System.out.println("list_fGObject");
    	this._004_show_list_fGObject(this.list_fGObject);
    }
    /* Function ACT003 : catching data of each list_fGObject we want to use, and add number
     * sequentially from start number - startNo */
    public String ACT003_getFileString(int startNo)
    {
    	String str = "";
    	int num = 0;
    	//add trajectory point of object
    	int index = 0;
    	while(index < this.nNumberOfPoint)
    	{
    		num = startNo + index;
    		str = str + num + " " + this.list_fGObject.get(index).ACT003_getFileString() + " t" + index;
    		if(index < this.nNumberOfPoint - 1)
    		{
    			str = str + "\n";
    		}
    		index = index + 1;
    	}
    	return str;
    }
    
    public void ACT004_addFGObject(FGObjectStructure newObj) {
    	if(this.list_fGObject == null) {
    		this.list_fGObject = new ArrayList<FGObjectStructure>();
    	}
    	this.list_fGObject.add(newObj);
    }
    
    public double ACT005_countStartEndDist() {
    	FGObjectStructure pointStart = this.GET003_elementIn_list_fGObject(0);
    	FGObjectStructure pointEnd = this.GET003_elementIn_list_fGObject(this.GET002_nNumberOfPoint() - 1);
    	
    	return Math.sqrt((pointStart.GET001_centerReal().x - pointEnd.GET001_centerReal().x)*(pointStart.GET001_centerReal().x - pointEnd.GET001_centerReal().x)
    			+ (pointStart.GET001_centerReal().y - pointEnd.GET001_centerReal().y)*(pointStart.GET001_centerReal().y - pointEnd.GET001_centerReal().y));
    }
    
    /* Function GET001 : get value of instance variable - nobjNewNumIndex */
    public int GET001_nobjNewNumIndex()
    {
    	return this.nobjNewNumIndex;
    }
    /* Function GET002 : get value of instance variable - nNumberOfPoint */
    public int GET002_nNumberOfPoint()
    {
    	return this.nNumberOfPoint;
    }
    public FGObjectStructure GET003_elementIn_list_fGObject(int index)
    {
    	return this.list_fGObject.get(index);
    }
    public void SET001_nNumberOfVideoFrame(int nNumberOfVideoFrame) {
		this.nNumberOfVideoFrame = nNumberOfVideoFrame;
	}
	public void SET002_nNumberOfSynopsisFrame(int nNumberOfSynopsisFrame) {
		this.nNumberOfSynopsisFrame = nNumberOfSynopsisFrame;
	}
	public void SET003_nNumberOfPoint(int nNumberOfPoint) {
		this.nNumberOfPoint = nNumberOfPoint;
	}
	public void SET004_NobjNewNumIndex(int nobjNewNumIndex) {
		this.nobjNewNumIndex = nobjNewNumIndex;
	}
	public void SET005_nNumberOfObject(int nNumberOfObject) {
		this.nNumberOfObject = nNumberOfObject;
	}
}
